/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wcbtest.docker.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcbtest.docker.domain.MachineDockerInfo;
import com.wcbtest.docker.domain.dto.DockerContainerInfoDTO;
import com.wcbtest.docker.domain.dto.DockerImageInfoDTO;
import com.wcbtest.docker.domain.dto.DockerNetworkInfoDTO;
import com.wcbtest.docker.domain.dto.DockerVolumeInfoDTO;
import com.wcbtest.docker.service.MachineDockerInfoService;
import com.wcbtest.docker.util.DockerClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@Api(tags = "docker基础接口")
@RequestMapping("/docker")
public class DockerController {

    @Resource
    private MachineDockerInfoService machineDockerInfoService;


    @ApiOperation(value = "docker数据库插入")
    @PostMapping(value = "/dockerInsert")
    public Object dockerInsert(@RequestBody MachineDockerInfo machineDockerInfo) {
        boolean save = machineDockerInfoService.save(machineDockerInfo);
        return save;
    }

    @ApiOperation(value = "docker数据库列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = false),
            @ApiImplicitParam(name = "name", value = "名称", required = false),
    })
    @PostMapping(value = "/dockerList")
    public Object dockerList(@RequestBody MachineDockerInfo machineDockerInfo) {
        QueryWrapper<MachineDockerInfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(machineDockerInfo.getHost())) {
            queryWrapper.like("host", machineDockerInfo.getHost());
        }
        if (StringUtils.isNotBlank(machineDockerInfo.getName())) {
            queryWrapper.like("name", machineDockerInfo.getName());
        }
        return machineDockerInfoService.list(queryWrapper);
    }

    @ApiOperation(value = "获取docker版本信息")
    @GetMapping(value = "/dockerVersion")
    public Object dockerInfo() {
        DockerClientUtils docker = getDockerClientUtils();
        return docker.getVersion();
    }


    @ApiOperation(value = "dockerr容器列表(包括暂停)")
    @PostMapping(value = "/dockerContainerList")
    public Object dockerContainerList(@RequestBody DockerContainerInfoDTO dockerContainerInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        List<DockerContainerInfoDTO> allContainer = docker.filterContainers(dockerContainerInfoDTO);
        return allContainer;
    }

    @ApiOperation(value = "docker容器启动")
    @PostMapping(value = "/dockerContainerStart")
    public Object dockerContainerStart(@RequestBody DockerContainerInfoDTO dockerContainerInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        docker.startContainer(dockerContainerInfoDTO.getId());
        return "启动docker容器";
    }


    @ApiOperation(value = "docker容器停止")
    @PostMapping(value = "/dockerContainerStop")
    public Object dockerContainerStop(@RequestBody MachineDockerInfo machineDockerInfo) {
        DockerClientUtils docker = getDockerClientUtils();
        docker.stopContainer(machineDockerInfo.getId());
        return "停止docker容器";
    }

    @ApiOperation(value = "docker容器重启")
    @PostMapping(value = "/dockerContainerRestart")
    public Object dockerContainerRestart(@RequestBody MachineDockerInfo machineDockerInfo) {
        DockerClientUtils docker = getDockerClientUtils();
        docker.restartContainer(machineDockerInfo.getId());
        return "docker容器重启";
    }


    @ApiOperation(value = "获取docker容器日志")
    @PostMapping(value = "/dockerContainerLog")
    public Object dockerContainerLog(@RequestBody DockerContainerInfoDTO dockerContainerInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        List<String> containerLogs = docker.getContainerLogs(dockerContainerInfoDTO.getId(), dockerContainerInfoDTO.getLogTailLineCount());
        return containerLogs;
    }

    @ApiOperation(value = "docker镜像列表")
    @PostMapping(value = "/dockerImagesList")
    public Object dockerImagesList(@RequestBody DockerImageInfoDTO dockerImageInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        List<DockerImageInfoDTO> containerLogs = docker.dockerImagesList(dockerImageInfoDTO);
        return containerLogs;
    }

    @ApiOperation(value = "docker卷列表")
    @PostMapping(value = "/dockerVolumeList")
    public Object dockerVolumeList(@RequestBody DockerVolumeInfoDTO dockerVolumeInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        List<Map> containerLogs = docker.dockerVolumeList(dockerVolumeInfoDTO);
        return containerLogs;
    }
    @ApiOperation(value = "docker网络列表")
    @PostMapping(value = "/dockerNetworkList")
    public Object dockerNetworkList(@RequestBody DockerNetworkInfoDTO dockerNetworkInfoDTO) {
        DockerClientUtils docker = getDockerClientUtils();
        List<DockerNetworkInfoDTO> containerLogs = docker.dockerNetworkList(dockerNetworkInfoDTO);
        return containerLogs;
    }


    //@ApiOperation(value = "获取docker信息")
    //@GetMapping(value = "/dockerInfo")
    //public Object dockerInfo(MachineDockerInfo machineDockerInfo) {
    //    DockerClientUtils docker = getDockerClientUtils();
    //    DockerContainerNumDTO info = docker.getDockerInfo();
    //    List<DockerContainerInfoDTO> allContainer = docker.getAllContainer();
    //
    //
    //    HashMap<String, Object> map = new HashMap<>();
    //    map.put("1、初始化Docker连接", docker);
    //    map.put("2、获取Docker容器数量信息", info);
    //    map.put("3、获取所有Docker容器(运行中和已停止)信息", allContainer);
    //    return map;
    //}


    private DockerClientUtils getDockerClientUtils() {
        //[{"id":"be6838cc393e8008331231f4f7aa5361","host":"tcp://192.168.110.130:2375","createTime":"2025-06-10T01:46:03.401+00:00","updateTime":null,"name":"11java测试docker"}]
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String machineDockerInfoStr = request.getHeader("machineDockerInfoId");
        MachineDockerInfo byId = machineDockerInfoService.getById(machineDockerInfoStr);
        if(StringUtils.isBlank(byId.getHost())){
            throw new RuntimeException("请求头缺失machineDockerInfoId参数");
        }
        DockerClientUtils docker = new DockerClientUtils(byId);
        return docker;
    }


}




